﻿using System.Collections.Generic;
using System.Linq;
using CatHouse.Controllers;
using CatHouse.Models;
using System.Web.Mvc;

namespace CatHouse.Areas.QuanLy.Controllers
{
    public class AdminCPController : BaseAdminController
    {
        // GET: QuanLy/AdminCP
        public ActionResult Index()
        {
            var list = Db.Users.ToList();
            return View(list);
        }

        [HttpGet]
        public ActionResult AddUser()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddUser([Bind(Include = "UserEmail, Password, Name, Photo, Infomation")] UserModel m)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    m.Password = GetMd5Hash(m.Password);
                    Db.Users.Add(m);
                    var changes = Db.SaveChanges();

                    if (changes > 0)
                    {
                        TempData["Message"] = string.Format("Tài khoản {0} được tạo thành công", m.UserEmail);
                        return RedirectToAction("Index");
                    }

                    ModelState.AddModelError("UserEmail", "Không thể tạo tài khoản.");
                }
                catch (System.Exception ex)
                {
                    ModelState.AddModelError("UserEmail", ex.Message);
                    return View(m);
                }
            }
            return View(m);
        }
    }
}